<layout name="layout"/>
<style>
    #ul_pic_list li { margin:5px;list-style: none;}
    #old_pic_list li {float: left;width:200px;height: 150px;margin: 5px;list-style-type: none;}
</style>


<div class="tab-div">
    <div id="tabbar-div">
        <p>
            <span class="tab-front" id="general-tab">通用信息</span>
            <span class="tab-back" >商品描述</span>
            <span class="tab-back" >会员价格</span>
            <span class="tab-back" >商品属性</span>
            <span class="tab-back" >商品相册</span>
        </p>
    </div>
    <div id="tabbody-div">
        <form enctype="multipart/form-data" action="__SELF__" method="post">
            <input type="hidden" name="id" value="<?php echo $data['id']; ?>">
            <input type="hidden" name="cat_data" value="<?php echo $data['cat_id'];?>">
            <table width="90%" id="general-table" class="tab_table" align="center">
                <tr>
                    <td class="label">商品分类：</td>
                    <td>
                        <select name="cat_id">
                            <option value="">选择分类</option>
                            <?php foreach($catData as $k => $v):
                            if($v['id'] == $data['cat_id'])
                                $select = 'selected="selected"';
                            else
                                $select = '';
                            ?>
                            <option <?php echo $select; ?> value="<?php echo $v['id']; ?>">
                                <?php echo str_repeat('-',8*$v['level']).$v['cat_name']; ?>
                            </option>
                            <?php endforeach;?>
                        </select><span class="require-field">*</span></td>
                    </td>
                </tr>

                <tr>
                    <td class="label">拓展分类 :
                        <input onclick="$('#cat_list').append($('#cat_list').find('li').eq(0).clone());" type="button" id="btn_add_cat" value="添加一个" />
                    </td>
                    <td>
                        <ul id="cat_list">
                        <?php if($gcData): ?>
                        <?php foreach($gcData as $k1 => $v1):?>
                        <li>
                            <select name="ext_cat_id[]">
                                <option value="">选择分类</option>
                                <?php foreach ($catData as $k => $v):
                                if($v['id'] == $v1['cat_id'])
                                    $select = 'selected="selected"';
                                else
                                    $select = '';
                                ?>
                                <option <?php echo $select;?> value="<?php echo $v['id'];?>"><?php echo str_repeat('-',8*$v['level']).$v['cat_name']; ?></option>
                                <?php endforeach; ?>
                            </select>
                            </li>
                            <?php endforeach;?>
                        <?php else: ?>
                            <li>
                                <select name="ext_cat_id[]">
                                    <option value="">选择分类</option>
                                    <?php foreach($catData as $k =>$v):?>
                                    <option value="<?php echo $v['id'];?>"><?php echo str_repeat('-',8*$v['level']).$v['cat_name']; ?>></option>
                                    <?php endforeach; ?>
                                </select>
                            </li>
                        <?php endif; ?>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td class="label">商品品牌：</td>
                    <td>
                        <?php buildSelect('brand','brand_id','id','brand_name',$data['brand_id']); ?>
                    </td>
                </tr>
                <tr>
                    <td class="label">商品名称：</td>
                    <td><input type="text" name="goods_name" value="<?php echo $data['goods_name']?>"size="30" />
                        <span class="require-field">*</span></td>
                </tr>
                <tr>
                    <td class="label">本店售价：</td>
                    <td>
                        <input type="text" name="shop_price" value="<?php echo $data['market_price']?>" size="20"/>
                        <span class="require-field">*</span>
                    </td>
                </tr>
                <tr>
                    <td class="label">市场售价：</td>
                    <td>
                        <input type="text" name="market_price" value="<?php echo $data['shop_price']?>" size="20" />
                    </td>
                </tr>
                <tr>
                    <td class="label">是否上架：</td>
                    <td>
                        <input type="radio" name="is_on_sale" value="1" <?php if($data['is_new']=='是') echo 'checked="checked"';?>/> 是
                        <input type="radio" name="is_on_sale" value="0" <?php if($data['is_new']=='否') echo 'checked="checked"';?>/> 否
                    </td>
                </tr>
                <tr>
                    <td class="label">促销价格：</td>
                    <td>
                        价格：<input type="text" name="promote_price" size="8" value="0.00"/>元
                        开始时间：<input type="text" id="promote_start_date" name="promote_start_date" value="<?php echo $data['promote_start_date']; ?>"/>
                        结束时间：<input type="text" id="promote_end_date" name="promote_end_date" value="<?php echo $data['promote_end_date']; ?>"/>
                    </td>
                </tr>
                <tr>
                    <td class="label">是否新品：</td>
                    <td>
                        <input type="radio" name="is_new" value="是" <?php if($data['is_new']=='是') echo 'checked="checked"';?>/>是
                        <input type="radio" name="is_new" value="否" <?php if($data['is_new']=='否') echo 'checked="checked"';?>/>否
                    </td>
                </tr>
                <tr>
                    <td class="label">是否热卖：</td>
                    <td>
                        <input type="radio" name="is_new" value="是" <?php if($data['is_hot']=='是') echo 'checked="checked"';?>/>是
                        <input type="radio" name="is_new" value="否" <?php if($data['is_hot']=='否') echo 'checked="checked"';?>/>否
                    </td>
                </tr>
                <tr>
                    <td class="label">是否精品：</td>
                    <td>
                        <input type="radio" name="is_new" value="是" <?php if($data['is_best']=='是') echo 'checked="checked"';?>/>是
                        <input type="radio" name="is_new" value="否" <?php if($data['is_']=='否') echo 'checked="checked"';?>/>否
                    </td>
                </tr>
                <tr>
                    <td class="label">是否推荐到楼层：</td>
                    <td>
                        <input type="radio" name="is_new" value="是" <?php if($data['is_floor']=='是') echo 'checked="checked"';?>/>是
                        <input type="radio" name="is_new" value="否" <?php if($data['is_floor']=='否') echo 'checked="checked"';?>/>否
                    </td>
                </tr>
                <tr>
                    <td class="label">商品图片：</td>
                    <td>
                        <input type="file" name="logo" size="35" />
                    </td>
                </tr>
            </table>

            </table>
            <!--  商品描述  -->
            <table style="display: none;" width="90%" class="tab_table" align="center">
                <tr>
                    <td>
                        <textarea id="goods_desc" name="goods_desc"></textarea>
                    </td>
                </tr>
            </table>

            <!--  会员价格  -->
            <table style="display: none;" width="90%" class="tab_table" align="center">
                <tr>
                    <td class="label">会员价格：</td>
                    <td>
                        <?php foreach($mlData as $k => $v): ?>
                        <?php echo $v['level_name'];?>: ￥<input type="text" name="member_price[<?php echo $v['id']?>]" size="8"
                    value="{$mpData[$v['id']]}"/>元<br>
                        <?php endforeach; ?>
                    </td>
                </tr>
            </table>
            <!--  商品属性  -->
            <table style="display: none;" width="90%" class="tab_table" align="center">
                <tr>
                    <td class="label">商品属性：</td>
                    <td>
                        <?php buildSelect('Type','type_id','id','type_name',$data['type_id']); ?>
                    </td></tr>
                <tr>
                    <td><ul id="attr_list">
                            <?php $attrId = array(); //所有出现过的属性ID?>
                            <?php foreach ($gaData as $k => $v): ?>
                            <?php if(in_array($v['attr_id'],$attrId))//判断属性ID第一次出现就是+,否则是-
                                $opt = '-';
                            else{
                            $opt = '+';
                            $attrId[] = $v['attr_id'];
                            } ?>
                        <li>
                            <input type="hidden" name="goods_attr_id[]" value="<?php echo $v['id']; ?>">
                            <?php if($v['attr_type'] == '可选'):?>
                            <a onclick = "addNewAttr(this);" href="#">[<?php echo $opt; ?>]</a>
                            <?php endif;?>
                            <?php echo $v['attr_name']; ?>
                            <?php if($v['attr_option_values']):
                                $attr = explode(',',$v['attr_option_values']);?>
                            <select>
                                <option value="">请选择</option>
                                <?php foreach($attr as $k1 => $v1):
                                    if($v1 == $v['attr_value'])
                                        $select = 'selected="selected"';
                                    else
                                    $select = '';
                                ?>
                                <option <?php echo $select;?> value="<?php echo $v1; ?>"><?php echo $v1; ?></option><?php endforeach;?>
                            </select>
                            <?php else: ?>
                            <input type="text" name="" value="<?php echo $v['attr_value']; ?>" /><br>
                            <?php endif; ?>
                        </li>
                    <?php endforeach; ?>
                    </ul></td>
                </tr>
            </table>

            <!--  商品相册  -->
            <table style="display: none;" width="90%" class="tab_table" align="center">
                <tr>
                    <td>
                        <input id="btn_add_pic" type="button" value="添加一张"/>
                        <hr/>
                        <ul id="ul_pic_list"></ul>
                        <hr/>
                        <ul id="old_pic_list">
                            <?php foreach ($gpData as $k => $v):?>
                            <li>
                                <input pic_id="<?php echo $v['id']; ?>" class="btn_del_pic" type="button" value="删除" /><br/>
                                <?php showImage($v['mid_pic'],150); ?>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                    </td>
                </tr>
            </table>

            <div class="button-div">
                <input type="submit" value=" 确定 " class="button"/>
                <input type="reset" value=" 重置 " class="button" />
            </div>
        </form>
    </div>
</div>

<div id="footer">
    共执行 9 个查询，用时 0.025161 秒，Gzip 已禁用，内存占用 3.258 MB<br />
    版权所有 &copy; 2005-2012 上海商派网络科技有限公司，并保留所有权利。
</div>
<script>

    $("select[name=type_id]").change(function () {
        var typeId = $(this).val();
        if (typeId > 0){
            $.ajax({
                type:"GET",
                url:"<?php echo U('ajaxGetAttr','',FALSE); ?>/type_id/"+typeId,
                dataType:"json",
                success:function (data) {
                    var li = '';
                    $(data).each(function (k,v) {
                        li += '<li>';
                        if(v.attr_type == '可选')
                            li += '<a onclick="addNewAttr(this);" href="#">[+]</a>';
                        //属性名称
                        li += v.attr_name + ':';
                        //如果属性有可选就做下拉框,
                        if(v.attr_option_values == ''){
                            li += '<input type="text" name="attr_value['+v.id+'][]">';
                        } else {
                            li += '<select name="attr_value['+v.id+'][]"><option value="">请选择... </option>';
                            var _attr = v.attr_option_values.split(',');
                            for (var i =0;i<_attr.length;i++){
                                li += '<option value="'+_attr[i]+'">';
                                li += _attr[i];
                                li += '</option>';
                            }
                            li += '</select>';
                        }
                        li += '</li>';
                    });
                    $("#attr_list").html(li);
                }
            });
        } else{
            $("#attr_list").html('');
        }
    });

    $("#tabbar-div p span").click(function () {
        var i = $(this).index();
        $(".tab_table").hide();
        $(".tab_table").eq(i).show();
        $(".tab-front").removeClass("tab-front").addClass("tab-back");
        $(this).removeClass("tab-back").addClass("tab-front");
    });

    $("#btn_add_pic").click(function () {
        var file = '<li><input type="file" name="pic[]" /></li>';
        $("#ul_pic_list").append(file);
    });
    
    $(".btn_del_pic").click(function () {
        if (confirm("确定要删除吗")){
            var li  = $(this).parent();
            var pid = $(this).attr('pic_id');
            $.ajax({
                type : "GET",
                url  : "<?php echo U('ajaxDelPic','',FALSE);?>/picid/"+pid,
                success : function (data) {
                    li.remove();
                }
            });
        } else{

        }
    });
    //
    function addNewAttr(a) {
        var li = $(a).parent();
        if($(a).text() == '[+]') {
            var newLi = li.clone();
            //去掉选中状态
            newLi.find("option:selected").removeAttr("selected");
            //克隆出来的的隐藏域的id清空
            newLi.find("input[name='goods_attr_id[]']").val("");
            newLi.find('a').text('[-]'); //+变成-
            li.after(newLi);
            var gaid = li.find("input [name='goods_attr_id[]']").val();
            if (gaid == ''){
                li.remove();
            }
        } else {
            if (confirm('如果删除了这个属性,那么相关的库存量数据也会被一起删除,确定要删除吗!')){
                $.ajax({
                    type:"GET",
                    url: "<?php echo U('ajaxDelAttr?goods_id='.$data['id'],'',FALSE);?>/gaid/"+gaid,
                    success:function (data) {
                        li.remove();
                    }
        })
            }
            li.remove();
        }
    }
</script>
<link href="__PUBLIC__/umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="__PUBLIC__/umeditor/third-party/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/umeditor/third-party/template.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/umeditor/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/umeditor/umeditor.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/umeditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
    UM.getEditor('goods_desc', {
        initialFrameWidth:'100%',
        initialFrameHeight:350
    });
</script>
</body>
</html>
